home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (c) 2009 Bui Viet Thanh (thanhbv@gmail.com).
- *
- * This file is part of clicknlearn.
- *
- * clicknlearn is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * clicknlearn is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with clicknlearn. If not, see <http://www.gnu.org/licenses/>.
- */
-
- Components.utils.import("resource://clicknlearn/ext/Observers.js");
- Components.utils.import("resource://clicknlearn/cnlprefs.js");
-
- var CNL_Options = {
- /**
- * currentDicIndex is currentRowIndex - 1
- */
- currentRowIndex: -1,
- rows: null,
- /**
- * Get indexOf elem in elem.parentNode.childNodes
- */
- _getChildIndex: function(elem){
- var e, k=0;
- for (e=elem; e = e.previousSibling; ++k){};
- return k;
- },
- eventLoadImpl: function(event){
- this.rows = document.getElementById("dics-rows");
- this.constructXul();
- addEventListener("focus", function(event){CNL_Options.eventFocusImpl(event);}, true);
- },
- eventFocusImpl: function(event){
- var parent = event.target.parentNode;
- if(!parent || parent.parentNode != this.rows)
- return;
- var newRowIndex = this._getChildIndex(parent);
- this._changeFocusedRow(newRowIndex);
- },
- _changeFocusedRow: function(newRowIndex){
- if(this.currentRowIndex == newRowIndex)
- return;
- this.rows.childNodes[newRowIndex].style.backgroundColor = "ThreeDShadow";
- if(this.currentRowIndex != -1)
- this.rows.childNodes[this.currentRowIndex].style.backgroundColor = "-moz-Dialog";
- if(this.currentRowIndex == 1)//change focus from first dic
- document.getElementById("buttonMoveUp").setAttribute("disabled", "false");
- if(newRowIndex == 1)//change focus to first dic
- document.getElementById("buttonMoveUp").setAttribute("disabled", "true");
- this.currentRowIndex = newRowIndex;
- },
- /**
- * construct options.xul dynamicly: add rows.
- */
- constructXul: function(){
- var dicts = CNL_Prefs.getDicsArrayPref();
- if(dicts.length == 0)
- dicts =
- [['true', 'tratu.vn', 'http://tratu.vn/dispatchaddon.php?dict=en_vn&title=',
- '<i>Bạn đang tra từ', '']
- ,['true', 'vdict.com', 'http://vdict.com/fsearch.php?dictionaries=eng2vie_vie2eng_foldoc&word=',
- ' <!-- Database --><!-- Database --><!-- Database -->Not Found.', '']
- ,['true', 'edictx.com', 'http://edictx.com/edictx/?dict=1&mode=addon&word=',
- ' was not found!<', '']
- ,['true', 'tudientiengviet.net', 'http://www.tudientiengviet.net/get.php?mode=quickdict&dict=en-vi&word=',
- 'Các từ tương tự: <br><ul>', '']];
- for(var i in dicts)
- this._showDictionary(dicts[i]);
- },
- _showDictionary: function(dic){
- var j, row, xbox, //checkbox or textbox
- //each row will contain 5 fields:
- //[".enable", ".name", ".baseUrl", ".notfoundPattern", ".selectors"],
- textboxesSize = ["13", "60", "35", "15"];
- row = document.createElement("row");
-
- xbox = document.createElement("checkbox");
- xbox.setAttribute("checked", dic[0] == "true");
- row.appendChild(xbox);
- for(j=1; j<5; j++){
- xbox = document.createElement("textbox");
- xbox.setAttribute("size", textboxesSize[j-1]);
- xbox.setAttribute("value", dic[j]);
- row.appendChild(xbox);
- }
- this.rows.appendChild(row);
- },
- addDictionary: function(){
- this._showDictionary(["true", "", "", "", ""]);
- this._changeFocusedRow(this.rows.childElementCount - 1);
- },
- /**
- * remove currentDic row & the corresponds preferences
- */
- removeDictionary: function(){
- // alert(this.currentRowIndex);
- // if(this.currentRowIndex <= 0)
- // return;
- if(this.rows.childElementCount == 2){
- alert("Can't remove all dictionary sources!");
- return;
- }
- var oldChild = this.rows.childNodes[this.currentRowIndex];
- if(this.currentRowIndex == this.rows.childElementCount - 1)
- this._changeFocusedRow(this.currentRowIndex - 1);
- else
- this._changeFocusedRow(this.currentRowIndex + 1);
- this.rows.removeChild(oldChild);
- },
- /**
- * moveUp current dictionary (current row)
- * condition: current dic must not be first dic
- * (we ensure this by disabling "Move Up" button if first dic got focus)
- */
- moveUpDictionary: function(){
- var currRow = this.rows.removeChild(this.rows.childNodes[this.currentRowIndex]);
- this.rows.insertBefore(currRow, this.rows.childNodes[this.currentRowIndex - 1]);
- this._changeFocusedRow(this.currentRowIndex - 1);
- },
- acceptDialog: function(){
- var dicservices = [];
- for(var i=1; i<this.rows.childElementCount; i++){
- var row = this.rows.childNodes[i];
- var dic = [row.childNodes[0].checked];
- for(j=1; j<5; j++)
- dic.push(row.childNodes[j].value);
- dicservices.push(dic);
- }
- CNL_Prefs.setDicsArrayPref(dicservices);
- Observers.notify("cnl_dicservicesPreferencesChanged");
- },
- validate: function(){
- var allDictDisabled = true;
- for(var i=1; i<this.rows.childElementCount; i++)
- if(this.rows.childNodes[i].childNodes[0].checked){
- allDictDisabled = false;
- break;
- }
- if(allDictDisabled){
- alert("Can't disable all dictionary sources!");
- return false;
- }
-
- return true;
- },
- /**
- * see: https://developer.mozilla.org/En/Code_snippets/Tabbed_browser
- */
- help: function(){
- var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
- .getService(Components.interfaces.nsIWindowMediator);
- var mainWindow = wm.getMostRecentWindow("navigator:browser");
-
- // Add tab, then make active
- mainWindow.gBrowser.selectedTab = mainWindow.gBrowser.addTab(
- "http://code.google.com/p/clicknlearn/wiki/UserGuide#Add_dictionary_sources");
- mainWindow.focus();
- }
- }
-
- window.addEventListener("load", function(){CNL_Options.eventLoadImpl();}, false);
-